package baiqitun.stupro.api.controller;

import baiqitun.stupro.common.exception.enums.support.NotFoundExceptionEnum;
import baiqitun.stupro.common.model.Result;
import baiqitun.stupro.common.model.TreeModel;
import baiqitun.stupro.common.util.IdsUtil;
import baiqitun.stupro.common.util.ResultUtil;
import baiqitun.stupro.common.validate.group.SaveGroup;
import baiqitun.stupro.common.validate.group.SaveRootGroup;
import baiqitun.stupro.common.validate.group.UpdateGroup;
import baiqitun.stupro.security.entity.SysPermission;
import baiqitun.stupro.security.model.vo.Ui;
import baiqitun.stupro.security.service.SysPermissionService;
import baiqitun.stupro.common.exception.support.NotFoundException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * @author kedaji
 */
@Api(description = "权限控制器")
@RequestMapping("/permission")
@RestController
public class PermissionController {
    @Autowired
    private SysPermissionService sysPermissionService;

//    public Result<List<TreeModel<SysPermission>>> trees(){
//
//    }

    @ResponseStatus(HttpStatus.OK)
    @ApiOperation("使用id获取一个指定的许可")
    @GetMapping("/{id}")
    public Result<SysPermission> getById(@ApiParam("许可Id") @PathVariable("id") String id){
        SysPermission permission = sysPermissionService.getById(id);
        if (permission == null){
            throw new NotFoundException(NotFoundExceptionEnum.ROLE_NOT_FOUND).put("id", id);
        }
        return ResultUtil.ok(permission);
    }

    @ResponseStatus(HttpStatus.OK)
    @ApiOperation("保存一条根许可")
    @PostMapping("/root")
    public Result<?> saveRootPermission(Authentication authentication,
                                        @ApiParam("许可") @Validated(SaveRootGroup.class) SysPermission permission){
        UserDetails userDetails = (UserDetails) authentication.getPrincipal();
        permission.setCreateBy(userDetails.getUsername());
        sysPermissionService.saveRoot(permission);
        return ResultUtil.ok();
    }

    @ResponseStatus(HttpStatus.OK)
    @ApiOperation("保存一条子许可")
    @PostMapping("/child")
    public Result<?> saveChildPermission(Authentication authentication,
                                         @ApiParam("许可") @Validated(SaveGroup.class) SysPermission permission){
        UserDetails userDetails = (UserDetails) authentication.getPrincipal();
        permission.setCreateBy(userDetails.getUsername());
        sysPermissionService.saveChild(permission);
        return ResultUtil.ok();
    }

    @ResponseStatus(HttpStatus.OK)
    @ApiOperation("删除一条许可")
    @DeleteMapping
    public Result<?> removeById(Authentication authentication,
                                @ApiParam("许可id") @NotBlank(message = "许可id不能为空") String id){
        UserDetails userDetails = (UserDetails) authentication.getPrincipal();
        sysPermissionService.removeById(id, userDetails.getUsername());
        return ResultUtil.ok();
    }

    @ResponseStatus(HttpStatus.OK)
    @ApiOperation("批量删除")
    @DeleteMapping("/batch")
    public Result<?> removeBatchById(Authentication authentication,
                                     @ApiParam("许可Id序列，由多个id使用逗号作为分隔符进行连接") @NotBlank(message = "序列不能为空") String ids){
        UserDetails userDetails = (UserDetails) authentication.getPrincipal();
        List<String> idList = IdsUtil.parser(ids);
        sysPermissionService.removeByIds(idList, userDetails.getUsername());
        return ResultUtil.ok();
    }

    @ResponseStatus(HttpStatus.OK)
    @ApiOperation("修改许可")
    @PutMapping
    public Result<?> updateById(Authentication authentication,
                                     @ApiParam("许可") @Validated(UpdateGroup.class) SysPermission permission){
        UserDetails userDetails = (UserDetails) authentication.getPrincipal();
        permission.setUpdateBy(userDetails.getUsername());
        sysPermissionService.updateById(permission);
        return ResultUtil.ok();
    }
}
